# Contents: 
# Purpose:
# Status:

require 'Song'
require 'File_Manager'

class Library
	def initialize(filename)
		@my_library = Array.new
		@my_library = File_Manager.load_file(filename)
	end
  
	def add_song(a)
		@my_library << a #stick a into the library
	end
  
	def print_library_debug
		@my_library.each do |s|
			print s.to_response
      			print "\r\n"
		end
	end

	def each
		@my_library.each do |s|
			return s
		end
	end
  
	def get_song(mhash,asong)
		@my_library.each do |s|
		 	if (s.hash == mhash) then
				return s
			end
		end
		return nil
	end
	def update_song(mhash, rating, playcount, lastplayed,msong)
		flag = 0
		@my_library.each do |s|
			if (s.hash == mhash)
				flag = 1
				if (rating!=0)
					s.set_rating(rating)
				end
				if (playcount != 0)
					s.increment_playcount(playcount)
				end
				if (lastplayed != 0)
					s.set_last_played(Time.now.strftime("%Y%m%d %H:%M:%S"))
				
				end
  			break
			end
		end
		if flag == 0 then
			add_song(msong)	
		end
	end
	
	def save(filename)
		print "Library / Save"
		File_Manager.save_file(@my_library, filename)
	end
end
